import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {ExtendHttpService} from "../../../common/extend-http.service";
import {TipService} from "../../services/tip.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-order-count-details',
  templateUrl: './order-count-details.component.html',
  styleUrls: ['./order-count-details.component.css']
})
export class OrderCountDetailsComponent implements OnInit {
  loading = true;
  isFlag = false;
  tipType = 'error';

  // 日志记录
  logList = [];

  // 支付方式
  optList = [];

  // 查看详情的参数
  isLoading = false; //  背景遮罩
  detailsShow = false; // 是否展示详情页
  details = {}; // 基本信息
  odList = []; // 已点菜品
  detailsFlag = true;

  @Input() id = '';
  @Output() closeDetail = new EventEmitter()
  constructor(
    private extendHttp: ExtendHttpService,
    private tipService: TipService,
    private router: Router
  ) { }

  ngOnInit() {

    this.extendHttp.post('/merchantApp/Statistics/getOrderEssentialInformation', {id: this.id}).subscribe(response => {
      let data = response.json();
      if (data.code == '0') {
        this.isLoading = false;
        this.details = data.orderForm;
        this.odList = data.odList;
        this.detailsShow = true;
        this.detailsFlag = true;
        this.optList = data.optList;
        data.orderLogList.forEach(v=>{
          let str = v.content;
          str = str.substr(0,2);
          v.stateStr = str;
        });
        this.logList = data.orderLogList;
      } else if (data.code == '-2001') {
        this.isLoading = false;
        this.tipType = 'error';
        this.detailsFlag = true;
        this.tipService.change(true, '登录超时，跳转到登录页面');
        setTimeout(() => {
          this.router.navigate(['/login']);
        }, 2000);
      } else {
        this.tipType = 'error';
        this.isLoading = false;
        this.detailsFlag = true;
        this.tipService.change(true, data.message);
      }
    }, error => {
      this.tipType = 'error';
      this.isLoading = false;
      this.detailsFlag = true;
      this.tipService.change(true, '系统繁忙，操作失败！');
    });
  }
  // 关闭详情页
  detailsClose() {
    this.detailsShow = false;
    this.isLoading = false;
    this.closeDetail.emit(true);
  }

}
